package com.atguigu.gmall.pms.service.impl;

import com.atguigu.core.bean.PageVo;
import com.atguigu.core.bean.Query;
import com.atguigu.core.bean.QueryCondition;
import com.atguigu.gmall.pms.dao.AttrAttrgroupRelationDao;
import com.atguigu.gmall.pms.dao.AttrDao;
import com.atguigu.gmall.pms.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gmall.pms.entity.AttrEntity;
import com.atguigu.gmall.pms.service.AttrService;
import com.atguigu.gmall.pms.vo.AttrVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Transient;
import org.springframework.stereotype.Service;

import java.util.List;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @Autowired
    private AttrDao attrDao;

    @Autowired
    private AttrAttrgroupRelationDao relationDao;

    @Override
    public PageVo queryPage(QueryCondition params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<AttrEntity>()
        );

        return new PageVo(page);
    }

    @Override
    public PageVo queryByCidTypePage(QueryCondition queryCondition, Integer attrType, Long cid) {
        // 构建查询条件
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("catelog_id", cid);
        if (attrType != null) {
            wrapper.eq("attr_type", attrType);
        }
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(queryCondition), // 构建分页条件
                wrapper
        );
        return new PageVo(page);
    }

    @Transient
    @Override
    public void saveAttrVo(AttrVo attr) {
        //新增规格
        int insert = this.attrDao.insert(attr);
        // 新增中间表
        AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
        relationEntity.setAttrId(attr.getAttrId());
        relationEntity.setAttrGroupId(attr.getAttrGroupId());
        this.relationDao.insert(relationEntity);
    }

    @Transient
    @Override
    public void updateAttrVoById(AttrVo attr) {
        int i = this.attrDao.updateById(attr);

        List<AttrAttrgroupRelationEntity> relationEntityList = relationDao.selectList(
                new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attr.getAttrId())
                        .eq("attr_group_id", attr.getAttrGroupId()));

        AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
        relationEntity.setId(relationEntityList.get(0).getId());
        relationEntity.setAttrId(attr.getAttrId());
        relationEntity.setAttrGroupId(attr.getAttrGroupId());

        this.relationDao.updateById(relationEntity);

    }

    @Override
    public AttrVo queryAttrVoByAttrId(Long attrId) {

        AttrEntity attrEntity = this.getById(attrId);

        AttrVo attrVo = new AttrVo();
        BeanUtils.copyProperties(attrEntity,attrVo);

        AttrAttrgroupRelationEntity relationEntity = relationDao.selectList(
                new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrId)).get(0);
        attrVo.setAttrGroupId(relationEntity.getAttrGroupId());
        return attrVo;
    }
}
